package com.glodon.paas.account.dao.mongo;

import static com.glodon.paas.consts.DatabaseConst.*;

import java.util.Date;
import java.util.List; 
 
import com.glodon.paas.account.dao.CommunicateBookDao;
import com.glodon.paas.account.dao.domain.CommunicateEntity;
import com.glodon.paas.exception.PaasServerExceptionFactory;
import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.UpdateOperations; 
 
public class CommunicateBookDaoMongoImpl extends MongoDBDao<CommunicateEntity, String> implements CommunicateBookDao {
	@Override
	public String insert(CommunicateEntity entity){
		if(entity.getId() == null){
			return null;
		}
		
		if(entity.getCreateTime() == null){
			entity.setCreateTime(new Date());
		} 
		
		entity.setUpdateTime(entity.getCreateTime());
		super.insert(entity);
		
		return entity.getId();		
	}
	
	@Override
	public int deleteAllByUserId(String userId) { 
		if(userId == null){
			return 0;
		}
		Query<CommunicateEntity> q = this.createQuery().filter("userId =", userId);
		return deleteByQuery(q);
	} 
	
	/**
	 * 1根据userId删除所有联系信息
	 * 2根据userId, contactId删除一条联系信息
	 */
	@Override
	public int deleteByContactId(String userId, String contactId) {
		if(userId == null || contactId == null){
			//TODO 数据不完整
			return 0;
		}
		
		Query<CommunicateEntity> q = this.createQuery().filter("userId =", userId);
		q.filter("contactId =", contactId);
		return super.deleteByQuery(q);
	}
	
	@Override
	public int deleteById(String id){
		return super.delete(id);
	}
	
	@Override
	public List<CommunicateEntity> selectByUserId(
			String userId,
			int orderType,
			int startIndex, 
			int count
			) {

		// 查询
		Query<CommunicateEntity> q = this.createQuery();
		q.filter("userId =", userId);
		if(ORDER_BY_ASC_INT == orderType){
			q.order("orderNum");
		}
		if(ORDER_BY_DESC_INT == orderType){
			q.order("-orderNum");
		}
		
		q.offset(startIndex).limit(count);	
		return q.asList();
	}
	
	@Override
	public long countByUserId(String userId) {
		Query<CommunicateEntity> q = this.createQuery().filter("userId =", userId);
		return q.countAll();
	}
 
	@Override
	public CommunicateEntity selectByContactId(String userId, String contactId) {
		if(userId == null || contactId == null){
			throw PaasServerExceptionFactory.validateException("userId od contactId is null", null);
		}
		
		Query<CommunicateEntity> q = this.createQuery();
		q.filter("userId =", userId);
		q.filter("contactId =", contactId);
		
		List<CommunicateEntity> list = q.asList();
		if(list.size() == 0){
			return null;
		}
		if(list.size() > 1){
			//TODO 自动更正，将其他的对象删除?先删除一条对象
			deleteById(list.get(1).getId());
		}
		return list.get(0);
	}

	public int updateById(CommunicateEntity communicate){
		if(communicate.getId() == null){
			throw PaasServerExceptionFactory.validateException("id is null", null);
		}
		
		UpdateOperations<CommunicateEntity> ops = createUpdateOperations();
		boolean update = false;
		if(communicate.getContactEmail() != null){
			ops.set("contactEmail", communicate.getContactEmail());
			update = true;
		}
		
		if(communicate.getRemarkName() != null){
			ops.set("remarkName", communicate.getRemarkName());
			update = true;
		}

		if(communicate.getRemarkText() != null){
			ops.set("remarkText", communicate.getRemarkText());
			update = true;
		}
		
		if(update){
			Query<CommunicateEntity> q = this.createQuery().filter("id =", communicate.getId());
			q.get();
			ops.set("updateTime", communicate.getUpdateTime());
			return updateFirst(q, ops);
		}	
		return 0;
	}

//	@Override
//	public int updateRemark(String id, String remarkName, String remarkText) {
//		CommunicateEntity entity = get(id);
//		if(entity == null){
//			return 0;
//		} 
//		
//		boolean update = false;
//		Query<CommunicateEntity> q = this.createQuery().filter("id =", id);
//		UpdateOperations<CommunicateEntity> ops = this.createUpdateOperations();
//		if(remarkName!=null){
//			ops.set("remarkName", remarkName);
//			update = true;
//		}
//		if(remarkText != null){
//			ops.set("remarkText", remarkText);
//			update = true;
//		}
//
//		if(update){
//			ops.set("updateTime", new Date());
//			this.updateFirst(q, ops);
//		}
//		
//		return 1;
//	}
}
